#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);

    turnHome();
}

MainWindow::~MainWindow()
{
    //EasyDown->terminate();
    delete ui;
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if (ReleaseCapture()) SendMessage(HWND(this->winId()), WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
    event->ignore();
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if (m_Drag && (event->buttons() & Qt::LeftButton))
    {
        move(event->globalPos() - m_DragPosition);
        event->accept();
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *)
{
    m_Drag=false;
}




void MainWindow::hideAllUi(){
    ui->logo->hide();
    ui->buttonStart->hide();
    ui->buttonBack->hide();
    ui->label->hide();
    ui->buttonAg->hide();
    ui->buttonAgLink->hide();
    ui->buttonCloud->hide();
    ui->buttonLocal->hide();
}

void MainWindow::turnHome(){
    hideAllUi();
    page=1;
    ui->logo->show();
    ui->buttonStart->show();
}

void MainWindow::turnSet(){
    hideAllUi();
    page=2;
    ui->buttonBack->show();
    ui->label->setText("用户协议");
    ui->label->show();
    ui->buttonAg->show();
    ui->buttonAgLink->show();
}

void MainWindow::turnMode(){
    hideAllUi();
    page=3;
    ui->buttonBack->show();
    ui->label->setText("选择镜像源");
    ui->label->show();
    ui->buttonCloud->show();
    ui->buttonLocal->show();
}

void MainWindow::turnCloud(){
    hideAllUi();
    page=41;
}

void MainWindow::turnLocal(){
    hideAllUi();
    page=42;
    ui->buttonBack->show();
    ui->label->setText("正在下载");
    ui->label->show();
}

//基础窗口套件
void MainWindow::on_buttonClose_clicked(bool checked)
{
    if (page==4){
        QMessageBox::StandardButton confireExit=QMessageBox::NoButton;
        if (QMessageBox::question(this,"确认要退出吗？","IDM仍在执行下载任务，退出制作程序后需要手动关闭IDM\n是否确认退出？",QMessageBox::Yes|QMessageBox::No,confireExit)==QMessageBox::No) return;
    }
    qApp->quit();
}

void MainWindow::on_buttonBack_clicked(bool checked)
{
    switch (page) {
    case 2:
        turnHome();
        break;
    case 3:
        turnSet();
        break;
    case 4:
        QMessageBox::StandardButton confirePause=QMessageBox::NoButton;
        if (QMessageBox::question(this,"确认要返回吗？","IDM仍在执行下载任务，返回后需要手动关闭IDM\n是否确认返回？",QMessageBox::Yes|QMessageBox::No,confirePause)==QMessageBox::Yes) turnSet();
        break;
    }
}
//1
void MainWindow::on_buttonStart_clicked(bool checked)
{
    turnSet();
}
//2
void MainWindow::on_buttonAgLink_clicked(bool checked)
{
    QDesktopServices::openUrl(QUrl(QLatin1String("https://wiki.edgeless.top/content/%E7%94%A8%E6%88%B7%E5%8D%8F%E8%AE%AE.md")));
}

void MainWindow::on_buttonAg_clicked(bool checked)
{
    turnMode();
}
//3
void MainWindow::on_buttonCloud_clicked(bool checked)
{
    mode=0;
    http h;
    QString versionISO=h.getVersion("ISO");
    qDebug()<<"getISOVersion:"<<versionISO;
}

void MainWindow::on_buttonLocal_clicked(bool checked)
{
    mode=1;
}
